local frostmourne_skill = fk.CreateSkill{
  name = "#frostmourne_skill",
  attached_equip = "frostmourne",
}

Fk:loadTranslationTable{
  [frostmourne_skill.name] = "霜之哀伤",

  ["#frostmourne-premeditate"] = "霜之哀伤：可以蓄谋任意张牌"
}

local U = require "packages/utility/utility"

frostmourne_skill:addEffect(fk.DamageCaused, {
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(frostmourne_skill.name) and player == target then
      return data.damageType ~= fk.IceDamage and data.card and table.contains({"slash","death_coil"}, data.card.trueName)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data.damageType = fk.IceDamage
    data.extra_data = data.extra_data or {}
    data.extra_data.frostmourne = true
  end
})

frostmourne_skill:addEffect(fk.AfterCardsMove, {
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(frostmourne_skill.name) then
      local room = player.room
      local use = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if use and use.data.card.trueName == "death_coil" then return true end
      local damage = room.logic:getCurrentEvent():findParent(GameEvent.Damage)
      if damage and damage.data.extra_data and damage.data.extra_data.frostmourne then return true end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local cards = {}
    table.forEach(data, function (move)
      table.forEach(move.moveInfo, function (info)
        if player.room:getCardArea(info.cardId) == Card.DiscardPile then
          table.insertIfNeed(cards, info.cardId)
        end
      end)
    end)

    local ids = player.room:askToChooseCards(player, {
      min = 0,
      max = 999,
      target = player,
      flag = { card_data = { { "pile_discard", cards }, } },
      skill_name = "frostmourne",
      prompt = "#frostmourne-premeditate"
    })

    if #ids > 0 then
      event:setCostData(self, ids)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local cards = event:getCostData(self) ---@type integer[]
    table.forEach(cards, function (id)
      if not player.dead then
        U.premeditate(player, id, "frostmourne", player)
      end
    end)
  end
})

return frostmourne_skill